module klang.编译.节点访问.访问节点_;
import 全局;
import klang.编译.核心.核心_: 整数_, 创建数组, 返回零值, 克隆数组;
import klang.编译.类型.枚举值.枚举值_: SK;
import klang.编译.语法节点.节点_: 基础节点_ = 节点_, 节点数组_;
alias Visitor = 基础节点_ delegate(基础节点_);
T visitNode(T)(T node = ··null!(T), Visitor visitor = ··null!(Visitor)) {
    if ((node is ··null!(T)) || (visitor is ··null!(Visitor))) {
        return node;
    }
    基础节点_ visited = visitor(node);
    if (visited !is ··null!(基础节点_)) {
        return (cast(T)(visited));
    }
    return ··null!(T);
}
节点数组_!(T) visitNodes(T)(节点数组_!(T) nodes = ··null!(节点数组_!(T)), Visitor visitor = ··null!(Visitor), long start = -2, long count = -2) {
    if ((nodes is null) || (nodes.原型 is ··null!(T[])) || (visitor is ··null!(Visitor))) {
        return nodes;
    }
    T[] updated;
    // Ensure start and count have valid values
    size_t 长度 = nodes.length;
    if (start == -2 || start < 0) {
        start = 0;
    }
    if (count == -2 || count > 长度 - start) {
        count = 长度 - start;
    }
    bool 有尾随逗号 = false;
    long 开始 = -1;
    long 结束 = -1;
    bool 已经初始数组 = false;
    if (start > 0 || count < 长度) {
        // If we are not visiting all 属于 the original nodes, we must always create a new array.
        // Since this is a fragment 属于 a node array, we do not copy over the previous location
        // and will only copy over `有尾随逗号` 如果 we are including the last element.
        updated = 创建数组!(T)();
        已经初始数组 = true;
        有尾随逗号 = nodes.有尾随逗号 && start + count == 长度;
    }
    {
        // Visit each original node.
        for (int i = 0; i < count; ++i) {
            T node = nodes[i + start];
            T visited;
            if (node !is ··null!(基础节点_)) {
                基础节点_ n = visitor(node);
                visited = n ? (cast(T)(n)) : 返回零值!(T)();
            }
            else {
                visited = 返回零值!(T)();
            }
            if (已经初始数组 || (visited !is ··null!(基础节点_)) || visited != node) {
                if (!已经初始数组) {
                    已经初始数组 = true;
                    updated = 克隆数组!(T)(cast(T[])(nodes)[0 .. i]);
                    有尾随逗号 = nodes.有尾随逗号;
                    开始 = nodes.开始;
                    结束 = nodes.结束;
                }
                if (visited !is ··null!(基础节点_)) {
                    .追加!(T)(updated, visited);
                }
            }
        }
    }
    if (已经初始数组) {
        节点数组_!(T) updatedArray = new 节点数组_!(T)(updated);
        updatedArray.开始 = 开始;
        updatedArray.结束 = 结束;
        updatedArray.有尾随逗号 = 有尾随逗号;
        return updatedArray;
    }
    return nodes;
}
